import Router from 'koa-router'
import config from '../config/index.js'
import sha1 from 'sha1'

export const router = app => {
  const router = new Router()

  router.get('/wechat-hear', (ctx, next) => {
    // console.log(ctx.query)
    const { signature, nonce, timestamp, echostr } = ctx.query
    const token = config.wechat.token
    const str = [token, timestamp, nonce].sort().join('')
    const sha = sha1(str)
    require('../wechat')

    if (sha === signature) {
      ctx.body = echostr
    } else {
      console.log(signature, sha)
      ctx.body = 'failed'
    }
  })
  router.post('/wechat-hear', (ctx, next) => {

  })

  app.use(router.routes())
  app.use(router.allowedMethods())
}
